home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / C⁄C++ / AIFF DSP v22 / plugin_src / analyze.c next >
Text File  |  1995-01-30  |  2KB  |  77 lines

  1. /* creates a histogram of the amplitudes present in a signal */
  2.  
  3. #include "plugin_specific.h"
  4. #include "aiff.h"
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include <string.h>
  8. #include <limits.h>
  9.  
  10. #define BINS_LOG2 4
  11. #define BINS (1<<BINS_LOG2)
  12. #define BIN_MASK (BINS-1)
  13.  
  14. static short min, max;
  15. static long histogram[BINS] = {0};
  16. static double sum_of_squares = 0.0;
  17.  
  18. void init_process_analyze( void ) {
  19.    if ( nh.wdsi != 8 && nh.wdsi != 16 )
  20.       err( "Cannot process a file with this word size" );
  21.    if ( USHRT_MAX != 65535 )
  22.       err( "this plugin is based on the assumption of 2 byte shorts" );
  23. }
  24.  
  25. void term_process_analyze( void ) {
  26. #define BARMAX 60
  27.  
  28.    double rms;
  29.    int i, barlen;
  30.    long hmax = 0;
  31.    char bar[BARMAX+1];
  32.  
  33.    memset( bar, '*', BARMAX );
  34.    
  35.    rms = sqrt( sum_of_squares / (nh.chan * nh.fram) );
  36.    
  37.    for (i=0; i<BINS; i++)
  38.       if ( histogram[i] > hmax ) hmax = histogram[i];
  39.    
  40.    printf( "rms val: %.1f\nmin: %6hd\nmax: %6hd\n", rms, min, max );
  41.    
  42.    for (i=0; i<BINS; i++) {
  43.       barlen = BARMAX * (((double)histogram[i])/hmax);
  44.       printf( "bin %4d: %8lX %.*s\n", i-BINS/2, histogram[i], barlen, bar );
  45.    }
  46. }
  47.  
  48. void process_samdat_analyze( long buflen ) {
  49.    static char first_time = 1;
  50.    short *tds, samval;
  51.    char  *tdc;
  52.    long i, ofs_samval, bin;
  53.  
  54.    tds = d;
  55.    tdc = d;
  56.    if ( first_time )
  57.    {
  58.       min = max = (nh.wdsi==8) ? *tdc : *tds;
  59.       first_time = 0;
  60.    }
  61.    for (i=0; i < buflen; i++)
  62.    {
  63.       samval = (nh.wdsi==8) ? *tdc++ : *tds++;
  64.       if      ( samval < min ) min = samval;
  65.       else if ( samval > max ) max = samval;
  66.       sum_of_squares += ((long) samval) * samval;
  67.  
  68.       ofs_samval = samval + (1L << (nh.wdsi-1));
  69.       bin = ofs_samval >> ( nh.wdsi - BINS_LOG2 );
  70.       if ( (bin & BIN_MASK) != bin ) err( "bin calculation gone wild" );
  71.       histogram[ bin ]++;
  72.    }
  73. }
  74.  
  75. plugin_info plugin_analyze = { init_process_analyze,
  76.    term_process_analyze, process_samdat_analyze, 1, 0 };
  77.